//
//  HeaderVC.m
//  顶部放大视图
//
//  Created by 徐荘 on 2017/3/7.
//  Copyright © 2017年 徐荘. All rights reserved.
//

#import "HeaderVC.h"
#import "YYWebImage.h"
#import "UIColor+Extension.h"

NSString *const cellID = @"cell";
#define kHeaderHeight 200
@interface HeaderVC ()<UITableViewDelegate , UITableViewDataSource>

/** <#备注#> */
@property (nonatomic,weak) UIView *headerView;
/**  */
@property (nonatomic,weak) UIImageView *imageV;
/** <#备注#> */
@property (nonatomic,weak) UIView *lineView;

/**
 <#Description#>
 */
@property(nonatomic,assign)UIStatusBarStyle statusBar;

@end

@implementation HeaderVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self prepareTableView];
    [self prepareHeaderView];
    
    self.statusBar = UIStatusBarStyleLightContent;
}

// 视图将要出现的时候调用该方法
- (void)viewWillAppear:(BOOL)animated{
    //隐藏 navigationBar
    [self.navigationController setNavigationBarHidden:YES];
    
    // 自动调整滚动视图边距： 在 viewController+NAV 中会自动调整 tableView 的 contentInset
    self.automaticallyAdjustsScrollViewInsets = NO;
}




//设置白色状态栏
- (UIStatusBarStyle)preferredStatusBarStyle{
    return _statusBar;
}

//准备顶部视图
- (void)prepareHeaderView{
    
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, kHeaderHeight)];
    
    headerView.backgroundColor = [UIColor xz_colorWithHex:0xF8F8F8];
    
    UIImageView *imageV = [[UIImageView alloc] initWithFrame:headerView.bounds];
    
     imageV.backgroundColor = [UIColor redColor];
    
    NSURL *url = [NSURL URLWithString:@"http://www.who.int/entity/campaigns/immunization-week/2015/large-web-banner.jpg?ua=1"];
    
    [ imageV yy_setImageWithURL:url options:1];
    
    //设置内容填充模式
    imageV.contentMode = UIViewContentModeScaleAspectFill;
    //设置裁剪
    imageV.clipsToBounds = YES;
    
    //创建分隔线
    CGFloat lineH = 1 / [UIScreen mainScreen].scale;
    UIView *lineV = [[UIView alloc] initWithFrame:CGRectMake(0, kHeaderHeight - lineH, headerView.bounds.size.width, lineH)];
    lineV.backgroundColor = [UIColor blackColor];
    
    self.headerView = headerView;
    self.imageV = imageV;
    self.lineView = lineV;

    [headerView addSubview:self.lineView];
    [headerView addSubview: self.imageV];
    [self.view addSubview:self.headerView];
}

//即将滚动时候调用
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    CGFloat offset = scrollView.contentOffset.y + scrollView.contentInset.top;
    NSLog(@"offset:%f",offset);
    
    if (offset < 0) {
        //向下放大
        self.headerView.x = 0;
        self.headerView.height = kHeaderHeight - offset;
        self.imageV.alpha = 1;
        
    }else{
        //向上移动
        self.headerView.height = kHeaderHeight;
        
        CGFloat min = kHeaderHeight - 64;
        self.headerView.y = -MIN(min, offset);
        
        CGFloat progress = 1 - (offset / min);
        self.imageV.alpha = progress;
        self.statusBar = (progress < 0.5) ? UIStatusBarStyleDefault : UIStatusBarStyleLightContent;
        //主动更新状态栏：需要 statusBar 外观更新
        [self.navigationController setNeedsStatusBarAppearanceUpdate];
    }
    
    //设置图像高度
    self.imageV.height = self.headerView.height;
    self.lineView.y = self.headerView.height - self.lineView.height;
    
}


//准备 tableview
- (void)prepareTableView {
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    
    tableView.delegate = self;
    tableView.dataSource = self;
    
    //注册
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellID];
    
    //设置内边距
    tableView.contentInset = UIEdgeInsetsMake(kHeaderHeight, 0, 0, 0);
    //设置滑动指示器的偏移
    tableView.scrollIndicatorInsets = tableView.contentInset;
    
    [self.view addSubview:tableView];
    
}

#pragma mark -----------------------
#pragma mark :数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 100;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
    
    cell.textLabel.text = @(indexPath.row).stringValue;
    
    return  cell;
}



@end
